
/*获取用户菜单信息*/
function getUserMenuInfo(menuActionUrl){
	
	var menuList;
	
	/*正式模式获取菜单配置数据*/
	 $.ajax({
	 	url : menuActionUrl,
	 	type :'POST',
	 	dataType : "json",
	 	async : false,
	 	contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
	 	success:function(data){
	 		
	 		var children = data.data;
	 	
	 		if(!children) return;
	 		
	 		/*创建所有菜单*/
			createMenu(children);
			
			menuList = children;
	 	}
	 });
	 
	 return menuList;
	 
}

/*根据数据创建所有菜单*/
function createMenu(data){
	
	//顶级菜单所有菜单项加起来的宽度
	var topMenuLayerWidth = 0;
	
	//清空顶级菜单的ul
	$("#tf-topMenu ul").empty();
	
	/*遍历每个菜单项*/
	$.each(data , function(index ,item){
		
		//若是按钮 , 则忽略
		if(item.type == 1) return;
		
		//创建顶级菜单
		createTopMenu(item);
		
		//计算顶部菜单宽度
		topMenuLayerWidth += $("#tf-topMenu ul li").eq(index).outerWidth(true);
		
		//创建左边菜单项
		createLeftMenu(item);
		
	});
	
	/*设置顶级菜单无序列表ul的宽度*/
	$("#tf-topMenu ul").width(topMenuLayerWidth);
	
}

//创建顶部菜单
function createTopMenu(item){
	
	//若是按钮 , 则忽略
	if(item.type == 1) return;
	
	/*顶级菜单菜单项文本*/
	var topMenuItemTxt = '<li style="width:90px;" menuId="'+item.id+'" data="'+item.id+'" class="'+item.id+'">'+
							'<a href="'+(item.url && item.url != "" ? item.url : "javascript:void(0)")+'">'+
								'<i style="color:white;" class="mr5 icon '+(item.icon && item.icon != ""?item.icon :"")+'"></i>'+ 
								 item.name+
							 '</a>'+
							'<span class="tf-line"></span>'+
						'</li>';

	$("#tf-topMenu ul").append(topMenuItemTxt);
}


/*根据顶部菜单项的子菜单,创建左边菜单项*/
function createLeftMenu(data){
	
	//若没有子菜单则直接返回
	if(!data || !data.children) return;
	
	//子菜单集合
	var children = data.children;
	
	/*创建父菜单项和子菜单项*/
	$.each(children , function(index , item){
		
		//若是按钮 , 则忽略
		if(item.type == 1) return;
		
		//扩展一个parentMenuId
		item.parentMenuId = data.parentMenuId;
		
		//创建父级
		createLeftMenuParentNode(item);
		
		//若是有子菜单,只支持到3级菜单
		if(item.children){
			var nextChildren = item.children;
			for(var i = 0 ; i < nextChildren.length ; i ++){
				var nextItem = nextChildren[i];
				nextItem.parentMenuId = item.id;
				createLeftMenuSonNode(nextItem);
			}
		}
	});
	
}

//创建左侧菜单父节点
function createLeftMenuParentNode(item){
	
	//左侧菜单父节点文本
	var leftParentMenuItemTxt = '<li menuId="'+item.id+'" parentMenuId="'+item.parentMenuId+'" style="display:none" class="'+item.id+' tf-nav-item'+'" >' +
															'<a href="javascript:void(0);">' +
															'<i class="icon '+(item.icon && item.icon != ""?item.icon :"icon-list")+'"></i>' +
															'<span>'+item.name+'</span>' +
															'<i class="tf-nav-more icon-angle-down"></i></a>' +
															'<ul>' +
															'</ul>' +
													  '</li>';
	
	
	$("#tf-menu1>ul").append(leftParentMenuItemTxt);
	
}


//创建左侧菜单子节点
function createLeftMenuSonNode(item){
	
	//左侧菜单子节点文本
	var leftSonMenuItemTxt = '<li menuId="'+item.id+'" parentMenuId="'+item.parentMenuId+'" >'+
													'<a class="tf-a '  + item.id + '" data="' + item.url + '" href="javascript:void(0)">'+
													'<span class="">' +
													'<i class="icon '+(item.icon && item.icon != ""? item.icon + " mr10" :"")+'"></i>'+ 
														item.name  + 
														'<i class="" name="linkedIcon"></i>'+
													'</span>' +
													'</a>' +
												  '</li>';
	
	/*添加到父节点中去*/
	$("#tf-menu1 ul ."+item.parentMenuId+" ul").append(leftSonMenuItemTxt);
}


/*链接第一个子菜单项*/
function linkFirstSonMenuItem(data){
    $("."+data.menuId+">a").trigger('click');
    $("."+data.menuId+">a").find(".icon-angle-down").removeClass("icon-angle-down").addClass("icon-angle-up");
    $("."+data.menuId+">a").next().show();
    $("."+data.menuId).find("ul>li>a:eq(0)").trigger('click');
}

//上一个顶部菜单项
function prevMenuItem(){
    var lefts = $("#tf-topMenu ul").position().left;
    var boxw = $("#tf-topMenu ul").width();
    var w = $(".tf-m-nav").width();
    var s = parseInt(w/parseInt(boxw/$("#tf-topMenu ul li").length))*parseInt(boxw/$("#tf-topMenu ul li").length);
    $("#tf-topMenu ul").animate({"left":(lefts + s)});
    $(".tf-next").show();
}

//下一个顶部菜单项
function nextMenuItem(){
    var lefts = $("#tf-topMenu ul").position().left;
    var boxw = $("#tf-topMenu ul").width();
    var w = $(".tf-m-nav").width();
    var s = parseInt(w/parseInt(boxw/$("#tf-topMenu ul li").length))*parseInt(boxw/$("#tf-topMenu ul li").length)
    var allleft =  (s*2) + Math.abs(lefts);
    $("#tf-topMenu ul").animate({"left":(lefts + -s)});
    if(allleft >= boxw){
        $(".tf-next").hide();
    }
}

//链接第一个顶级菜单
function linkFirstTopMenuItem(){
	$("#tf-topMenu ul li:first").click();
}

//链接首页
function linkHomePage(homePageUrl){
	redirectLocal(homePageUrl);
}

//链接最后一次访问的菜单项
function linkLastAccessMenuItem(){
	
	//从缓存里取出最后一次访问的菜单项信息
	var lastAccessMenuItem = $.cookie("lastAccessMenuItem");
	
	/*若没有缓存信息则链接首页*/
	if(!lastAccessMenuItem){
		 //链接首页
	    linkHomePage(LocalCache.homePageUrl);
	    //链接第一个顶级菜单
	    linkFirstTopMenuItem();
	    return;
	}
	
	/*上一次缓存的菜单配置信息*/
	lastAccessMenuItem = JSON.parse(lastAccessMenuItem);

	//若是特殊处理的链接,直接跳转
	if(lastAccessMenuItem.special){
		//隐藏左边菜单
		hideLeftMenu(true);
		redirectLocal(lastAccessMenuItem.url);
		return;
	}
	
	/*获取菜单项信息*/
	var menuId = lastAccessMenuitem.id;
	var parentMenuId = lastAccessMenuItem.parentMenuId;
	var linkUrl = lastAccessMenuItem.url;
	var jqMenuItemLi = $("#tf-menu1").find('li[menuId="'+menuId+'"]');
	var jqParentMenuItemLi =  $("#tf-menu1").find('li[menuId="'+parentMenuId+'"]');
	var topMenuItemAclMenuId = jqParentMenuItemLi.attr("parentMenuId");
	var jqTopMenuItemLi = $("#tf-topMenu").find('li[menuId="'+topMenuItemAclMenuId+'"]');
	
	/*若找不到那个菜单项,也跳转首页*/
	if(!jqMenuItemLi || jqMenuItemLi.length == 0){
		 //链接首页
	    linkHomePage(LocalCache.homePageUrl)
	    //链接第一个顶级菜单
	    linkFirstTopMenuItem();
	    return;
	}
	
	/*展开顶级菜单*/
	jqTopMenuItemLi.click();
	
	/*链接目标页面*/
	jqParentMenuItemLi.click();
	jqMenuItemLi.find(".tf-a").click();
	
}

//去除所有菜单高亮状态
function removeAllMenuItemHighlight(){
	/*去除其它菜单项的高亮*/
    $("#tf-topMenu ul li").removeClass("tf-cur");
	//去除其它高亮的子菜单
    $(".tf-nv-active").removeClass("tf-nv-active");
    //子菜单左边的箭头去掉
    $(".icon-angle-right").removeClass("icon-angle-right");
}

//隐藏左边菜单
function hideLeftMenu(noAnimate){
	 if(noAnimate){
		 $("#tf-main").css("padding-left","0");
		 return;
	 }
	 $("#tf-main").animate({"paddingLeft":0});
}

//显示左边菜单
function showLeftMenu(noAnimate){
	if($("#tf-main").css("paddingLeft").split("px")[0] == 0){
		if(noAnimate){
			 $("#tf-main").css("padding-left","180");
			 return;
		 }
		$("#tf-main").animate({"paddingLeft":180})
	}
}

/*初始化*/
$(function(){
	
	 //加载菜单信息
    var menuList = getUserMenuInfo(LocalCache.menuActionUrl);
    
    //左边子菜单绑定事件
    $("#tf-menu1").find(".tf-a").on('click', function () {
    	
    	//链接地址
        var linkUrl =  $(this).attr("data");
        
    	/*父级li标签*/
    	var jqLi = $(this).parent("li");
    	
    	/*当前菜单项的id,和父级菜单的id*/
    	var menuId = jqLi.attr("menuId");
    	var parentMenuId = jqLi.attr("parentMenuId");
    	
    	//缓存本次点击的菜单项信息
    	var menuItemInfo = {"linkUrl" : linkUrl , "menuId" : menuId , "parentMenuId" : parentMenuId};
    	
		$.cookie("lastAccessMenuItem", JSON.stringify(menuItemInfo));
		
    	//去除其它高亮的子菜单
        $(".tf-nv-active").removeClass("tf-nv-active");
        
        //子菜单左边的箭头去掉
        $(".icon-angle-right").removeClass("icon-angle-right");
        
        //给前的子菜单项高亮并加上箭头
        $(this).find("span").addClass("tf-nv-active").find('i[name="linkedIcon"]').addClass("icon-angle-right");
        
      //若链接地址不存在,则直接返回
        if(!linkUrl || linkUrl == "" || linkUrl == "undefined"){
        	new $.zui.Messager('提示消息：链接不存在 , 请检查菜单项配置 !', {icon:'times',type: 'danger',placement: 'center',time:'1000'}).show();
        	return;
        } 
        
        //跳转链接地址
        redirectLocal(linkUrl);
        
    });

    //左边父子级菜单点击展开子菜单
    $(".tf-nav-item>a").on("click",function(){
        if($(this).next().is(":hidden")){
            $(this).find(".icon-angle-down").removeClass("icon-angle-down").addClass("icon-angle-up");
            $(this).next().show();
        }else{
            $(this).next().hide();
            $(this).find(".icon-angle-up").removeClass("icon-angle-up").addClass("icon-angle-down");
        }
    })
    
    //根据头部菜单宽度显示隐藏滚动图标
    $("#tf-topMenu ul").width()>$(".tf-m-nav").width()?$(".tf-prev,.tf-next").show():$(".tf-prev,.tf-next").hide();
    
    //头部滚动图标点击事件左
    $(".tf-prev").on("click",function(){
        var lefts = $("#tf-topMenu ul").position().left;
        if(lefts < 0 && !$("#tf-topMenu ul").is(":animated")){
        	prevMenuItem();
        }
    })
    
    //头部滚动图标点击事件右
    $(".tf-next").on("click",function(){
        if(!$("#tf-topMenu ul").is(":animated")){
        	nextMenuItem();
        }
    });
    
    //绑定顶部菜单点击事件
    $("#tf-topMenu ul li").on("click",function(){
    	
    	//显示左边菜单
    	showLeftMenu();
    	
    	//菜单ID
        var menuId = $(this).attr("data");
        
        /*去除其它菜单项的高亮*/
        $("#tf-topMenu ul li").removeClass("tf-cur");
        
        /*当前菜单项加上高亮*/
        $(this).addClass("tf-cur");
        
        if(!menuList) return;
        
        /*展开对应的左边菜单*/
        $.each(menuList , function(index,item){
        
        	/*若找到了对应的子菜单*/
            if(item.id == menuId){
            	
            	//隐藏左边菜单所有的菜单项
            	$(".tf-nav-item").hide();
            	
            	/*子菜单集合*/
            	var children = item.children;
            	
            	/*若有子节点才展示所有的子节点*/
            	if(children){
            		$.each(children , function(index,item){
                		/*显示所有左边菜单的父级节点*/
                		if(item){
                			$("."+item.id).show();
                		}
                	});
            	}
            	
            }
        });
        
    });
    
    //链接第一个顶级菜单
    linkFirstTopMenuItem();
    
    //内容中部 , 链接首页
    linkHomePage(LocalCache.homePageUrl);
    
});
